
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <signal.h>
#include <sys/resource.h>
#include <sys/uio.h>
#include <pwd.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <limits.h>
#include <malloc.h>
#include <sstream>
#include "AntDefine.h"
#include "Facade.h"
#include "NetUtils.h"
#include "Threads.h"
#include "HttpHandler.h"


int HttpHandler :: ReadProcess(conn *c)
{
	Facade *pR = Facade::Instance();
        ostringstream os;
        m_num++;
        if(m_num%100 == 0)
            printf("%ld\n", m_num);
	if(c->rbytes > 3 && strncmp("GET", c->rcurr, 3) == 0)
	{
		os<<"HTTP/1.1 200 OK\r\n";
		os<<"Content-Type: text/html; charset=utf-8\r\n";
		os<<"Date: Tue, 16 Sep 2008 14:23:45 GMT\r\n" ;
        	os<<"Content-Length:25\r\n";
		os<<"\r\n<html>Hello,world!</Html>";
            
		bcopy(os.str().c_str(), c->wbuf, os.str().length());
                c->wbytes = os.str().length();
		pR->GetNetWork()->write_block(c );
		return conn_closing;
	}
	else
	{
		printf("%s\n", c->rcurr);
		return conn_write;
	}         
}


int HttpHandler :: WriteProcess(conn *c)
{
	Facade *pR = Facade::Instance();
	ostringstream os;
	os<<"HTTP/1.1 200 OK\r\n";
	os<<"Content-Type: text/html; charset=utf-8\r\n";
	os<<"Date: Tue, 16 Sep 2008 14:23:45 GMT\r\n" ;
	os<<"Content-Length:25\r\n";
	os<<"\r\n<html>Hello,world!</Html>";
	bcopy(os.str().c_str(), c->wbuf, os.str().length());
	c->wbytes = os.str().length();
	pR->GetNetWork()->write_block(c );
	return conn_closing;
}
